#include "setting.h"

Setting::Setting()
{
    setDefaultValue();    //设置全局默认值
    isDirExist(QCoreApplication::applicationDirPath()+"\\"+"Configure");
}

bool Setting::isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
        return true;
    }
    else
    {
        bool ok = dir.mkdir(fullPath);//只创建一级子目录，即必须保证上级目录存在
        return ok;
    }
}

void Setting::init(QString name)
{
    ConfigureName=name;
    ConfigureFilepath=QCoreApplication::applicationDirPath()+"\\"+"Configure"+"\\"+name+".ini";
    set=QSharedPointer<QSettings>(new QSettings(ConfigureFilepath,QSettings::IniFormat));
}


bool Setting::ConfigurExist(QString path)
{
    QFileInfo file(ConfigureFilepath);
    if(file.exists())
    {
        return true;
    }
    else
    {
        return false;
    }
}


void Setting::Start()
{
    if(ConfigurExist(ConfigureFilepath))
    {
        qDebug()<<"找到配置文件!";
        ReadConfigure();
        qDebug()<<"设置参数完毕";
    }
    else
    {
        qDebug()<<"未找到配置文件,使用默认配置";
        SetDefaultConfigur(); //设置配置默认值
    }
}


void Setting::setDefaultValue()
{
    Common::ConfigureLock.lockForWrite();
    Common::Log_Open="true";
    Common::Log_Size=500;
    Common::TcpPort=60000;
    Common::TcpHandleThreadNum=3;
    Common::LocalPort="localServer";
    Common::SQL_IP="127.0.0.1";
    Common::SQL_DBname="distributed_sql";
    Common::SQL_UserName="root";
    Common::SQL_Password="root";
    Common::SQL_Port =3306;
    Common::Handle_Count=3;
    Common::distribution_multiple=2;
    Common::ConfigureLock.unlock();
}

void Setting::ReadConfigure()
{
    Common::ConfigureLock.lockForWrite();
    if(!set.data()->value("Log/Log_Open").isNull())
    {
        bool Log_Open=set.data()->value("Log/Log_Open").toBool();
        Common::Log_Open=Log_Open;
        if(!Log_Open)
        {
            Common::Logdata.clear();
        }
    }
    if(!set.data()->value("Log/Log_Size").isNull())
    {
        bool ok;
        int Log_Size=set.data()->value("Log/Log_Size").toInt(&ok);
        if(ok)
        {
            Common::Log_Size=Log_Size;
        }
    }
    if(!set.data()->value("TcpServer/Port").isNull())
    {
        bool ok;
        int TcpPort=set.data()->value("TcpServer/Port").toInt(&ok);
        if(ok)
        {
            Common::TcpPort=TcpPort;
        }
    }

    if(!set.data()->value("TcpServer/TcpHandleThreadNum").isNull())
    {
        bool ok;
        int TcpHandleThreadNum=set.data()->value("TcpServer/TcpHandleThreadNum").toInt(&ok);
        if(ok)
        {
            Common::TcpHandleThreadNum=TcpHandleThreadNum;
        }
    }

    if(!set.data()->value("LocalServer/Port").isNull())
    {
        Common::LocalPort=set.data()->value("LocalServer/Port").toString();
    }
    if(!set.data()->value("MYSQL/IP").isNull())
    {
        Common::SQL_IP=set.data()->value("MYSQL/IP").toString();
    }
    if(!set.data()->value("MYSQL/SQL-DBNAME").isNull())
    {
        Common::SQL_DBname=set.data()->value("MYSQL/SQL-DBNAME").toString();
    }
    if(!set.data()->value("MYSQL/SQL-UserName").isNull())
    {
        Common::SQL_UserName=set.data()->value("MYSQL/SQL-UserName").toString();
    }
    if(!set.data()->value("MYSQL/SQL-Password").isNull())
    {
        Common::SQL_Password=set.data()->value("MYSQL/SQL-Password").toString();
    }
    if(!set.data()->value("MYSQL/SQL_Port").isNull())
    {
        Common::SQL_Port=set.data()->value("MYSQL/SQL_Port").toInt();
    }
    if(!set.data()->value("Handle/Handle-Count").isNull())
    {
        bool ok;
        int Handle_Count=set.data()->value("Handle/Handle-Count").toInt(&ok);
        if(ok)
        {
            Common::Handle_Count=Handle_Count;
        }
    }
    if(!set.data()->value("CTask/distribution-multiple").isNull())
    {
        bool ok;
        int Handle_Count=set.data()->value("CTask/distribution-multiple").toInt(&ok);
        if(ok)
        {
            Common::distribution_multiple=Handle_Count;
        }
    }

    Common::ConfigureLock.unlock();
}

void Setting::SetDefaultConfigur()
{
    set.data()->setValue("Log/Log_Open","true");
    set.data()->setValue("Log/Log_Size","500");
    set.data()->setValue("TcpServer/Port","60000");
    set.data()->setValue("TcpServer/TcpHandleThreadNum","3");
    set.data()->setValue("LocalServer/Port","localServer");
    set.data()->setValue("MYSQL/IP","127.0.0.1");
    set.data()->setValue("MYSQL/SQL-DBNAME","distributed_sql");
    set.data()->setValue("MYSQL/SQL-UserName","root");
    set.data()->setValue("MYSQL/SQL-Password","root");
    set.data()->setValue("MYSQL/SQL_Port",Common::SQL_Port);
    set.data()->setValue("Handle/Handle-Count","3");
    set.data()->setValue("CTask/distribution-multiple","2");
}
